package com.yuanss.smart.fdemo.demo;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description Shiro Demo
 * @Date 2024/11/19 下午5:10
 * @Created by 菜到不敢run
 */
public class ShiroDemo {

    private static final Logger logger = LoggerFactory.getLogger(ShiroDemo.class);
    public static void main(String[] args) {
        //初始化 SecurityManager
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);

        //获取当前用户
        Subject subject = SecurityUtils.getSubject();

        // 登录
        UsernamePasswordToken token = new UsernamePasswordToken("shiro", "201314");
        try {
            subject.login(token);
        } catch (AuthenticationException e) {
            logger.error("登录失败", e);
            return;
        }
        logger.info("登录成功!Hello " + subject.getPrincipal());

        // 退出
        subject.logout();
    }
}
